<?php

namespace App\Http\Controllers\Api;

use App\Jobs\Subpackage;
use App\Repository\UpThree\MainOrderRepostition;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class TelecomOldController extends BaseController
{
    public function __construct()
    {
        $this->log = new Logger('TelecomOldPush');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/TelecomoldPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    public function pushOrderStatus(Request $Request)
    {
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        if (empty($getData)) {
            $this->log->info('报错信息查找33333333' . json_encode($getData['report']));
            return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
        }
        $getData = json_decode($getData, true);
        if (empty($getData['report'])) {
            $this->log->info('报错信息查找2222' . json_encode($getData['report']));
            return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
        }
        $data = [
            'is_send' => 0,
            'msg' => json_encode($getData),
            'model' => 'old_telecom',
            'created_at' => date('Y-m-d H:i:s')
        ];
        $tableId = DB::table('push_data')->insertGetId($data);
        dispatch(new Subpackage($tableId, 'readPushData'))->onQueue('TaskLongFileDispose');
        return 1;
    }

    // 电信那边推送过来的数据
    public function pushOrderStatus_back(Request $Request)
    {
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        // return 1;
        // var_dump(1111111111);
        // $getData = '{"logisticsCompanies":"顺丰快递","sign":"64B92D7B8B8D027CCB52D5574068C3F9","phoneNum":"F95662BD5703BBDB8AA03793A08E8EDA","ordId":"Hk_20220521170443374284738314","orderStatusDescribe":"订单完成","orderState":"D1002","requestTime":1653235229000,"areaCode":"0399","balanceOne":"20000.0","identityId":"B0E29D818D5B4EA9848EA75EB4F4D91DFEF0D36AED32723F0DCF2577C22A7D1B","name":"029CE5B6800B31E3DECAC5493CE768E7","uim":"5674320039F2CA563E9C5FCC112B970F9411186656B275C747F1B7262C45D3DF","logisticsNo":"4BED77E946003D610086CEA5B264535F","state":"100000","describe":"激活成功","stateDate":1653216306000}';
        // $getData = '{"report":[{"order":"SN_202205271150311976","order_num":"HK_2022052711503173622965322","express_company":"\u987a\u4e30\u5feb\u9012","iccid":"17739555689","express_num":"031849603257","status":4}]}';
        // 数据处理
        if (empty($getData)) {
            $this->log->info('报错信息查找33333333' . json_encode($getData['report']));
            return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
        }
        $getData = json_decode($getData, true);
        if (empty($getData['report'])) {
            $this->log->info('报错信息查找2222' . json_encode($getData['report']));
            return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
        }
        $statusData = [
            '1' => '1', //已激活
            '4' => '20', // 已发货
            '5' => '30', //已签收
            '6' => '40', // 已激活
            '13' => '60'
        ];
        $model = new MainOrderRepostition();
        foreach ($getData['report'] as $key => $v) {
            if (!isset($statusData[$v['status']])) {
                $this->log->info('报错信息查找' . json_encode($v));
                continue;
            }
            $this->log->info('报错信息查找3333' . json_encode($v));
            // 将数据格式 修改成固定的数据格式
            $editData = [
                'status' => !empty($statusData[$v['status']]) ? $statusData[$v['status']] : '50',
                'express_company' => !empty($v['express_company']) ? $v['express_company'] : '',
                'express_number' => !empty($v['express_num']) ? $v['express_num'] : '',
                'three_msg' => !empty($v['describe']) ? $v['describe'] : '',
                'iccid' => !empty($v['iccid']) ? $v['iccid'] : '',
                'first_status' => !empty($v['balanceOne']) ? '1' : '0',
                'first_status_desc' => !empty($v['balanceOne']) ? ($v['balanceOne'] / 100) : '0',
            ];
            $where = ['orderno' => $v['order_num']];
            $this->log->info('老系统修改数据' . json_encode($where) . '参数' . json_encode($editData));
            $res = $model->followOrderStatus($where, $editData);
            $this->log->info('老系统通知参数' . json_encode($v) . '返回参数' . json_encode($res));
        }
        return 1;

        return json_encode(['responseCode' => 1, 'result' => '数据处理失败']);
    }
}
